
package com.ebupt.migu.music.global.service.impl;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ebupt.migu.globalpopup.entity.ActivityInfo;
import com.ebupt.migu.globalpopup.entity.ApprovedMemo;
import com.ebupt.migu.globalpopup.entity.H5Info;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.DateUtil;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.global.dto.H5InfoDTO;
import com.ebupt.migu.music.global.mapper.ActivityInfoMapper;
import com.ebupt.migu.music.global.mapper.H5InfoMapper;
import com.ebupt.migu.music.global.service.IApprovedMemoService;
import com.ebupt.migu.music.global.service.IH5InfoService;
import com.ebupt.migu.music.global.vo.AuditVO;
import com.ebupt.migu.music.global.vo.H5InfoVO;
import com.ebupt.migu.music.global.wrapper.H5InfoWrapper;
import com.ebupt.migu.music.global.wrapper.Query;
import com.ebupt.migu.music.system.entity.SysUser;


/**
 * h5活动连接表 服务实现类
 *
 * @author Blade
 * @since 2021-11-26
 */
@Service
public class H5InfoServiceImpl extends ServiceImpl<H5InfoMapper, H5Info> implements IH5InfoService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private IApprovedMemoService iApprovedMemoService;
    @Autowired
    private ActivityInfoMapper activityInfoMapper;

    @Override
    public ResultObject detail(String id) {
        ResultObject<Object> objectResultObject = new ResultObject<>();
        H5Info h5InfoServiceById = this.getById(id);
        H5InfoVO h5InfoVO = H5InfoWrapper.build().entityVO(h5InfoServiceById);
        if (h5InfoVO.getActivityId() != null && !h5InfoVO.getActivityId().isEmpty()) {
            List<String> collect = Arrays.stream(h5InfoVO.getActivityId().split(",")).map(s -> {
                ActivityInfo activityInfo = activityInfoMapper.selectOne(new QueryWrapper<ActivityInfo>()
                        .lambda()
                        .eq(true, ActivityInfo::getActivityId, s));
                return activityInfo.getActivityName();
            }).collect(Collectors.toList());
            h5InfoVO.setActivityNames(collect);
        }
        objectResultObject.setCode("0000");
        objectResultObject.setMsg("成功");
        objectResultObject.setData(h5InfoVO);
        return objectResultObject;
    }

    @Override
    public ResultObject auditDetail(String id) {
        ResultObject<Object> resultObject = new ResultObject<>();
        List<ApprovedMemo> list = iApprovedMemoService.list(new QueryWrapper<ApprovedMemo>().lambda()
                .eq(true, ApprovedMemo::getH5Id, id));
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        resultObject.setData(list);
        return resultObject;
    }

    @Override
    public ResultObject page(Query query) {
        ResultObject<Object> resultObject = new ResultObject<>();
        IPage<H5Info> page = this.page(new Page<>(query.getCurrent(), query.getEvery(), true));
        IPage<H5InfoVO> h5InfoVOIPage = H5InfoWrapper.build().pageVO(page);
        if (h5InfoVOIPage.getRecords() != null && !h5InfoVOIPage.getRecords().isEmpty()) {
            List<H5InfoVO> Records = h5InfoVOIPage.getRecords().stream().map(h5InfoVO -> {
                if (h5InfoVO.getActivityId() != null && !h5InfoVO.getActivityId().isEmpty()) {
                    List<String> collect = Arrays.stream(h5InfoVO.getActivityId().split(",")).map(s -> {
                        ActivityInfo activityInfo = activityInfoMapper.selectOne(new QueryWrapper<ActivityInfo>()
                                .lambda()
                                .eq(true, ActivityInfo::getActivityId, s));
                        return activityInfo.getActivityName();
                    }).collect(Collectors.toList());
                    h5InfoVO.setActivityNames(collect);
                }
                return h5InfoVO;
            }).collect(Collectors.toList());

            h5InfoVOIPage.setRecords(Records);
        }
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        resultObject.setData(h5InfoVOIPage);
        return resultObject;
    }

    @Override
    public ResultObject auditPage(Query query) {
        ResultObject resultObject = new ResultObject<>();
        QueryWrapper<H5Info> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(query.getApproveStatus() != null&&!query.getApproveStatus().equals(""), H5Info::getStatus, query.getApproveStatus())
                .apply(query.getStartTime() != null&&!query.getStartTime().equals(""), "activity_start >'" + query.getStartTime() + "'")
                .apply(query.getEndTime() != null&&!query.getEndTime().equals(""), "activity_start < '" + query.getEndTime() + "'");
        IPage<H5Info> page = this.page(new Page<>(query.getCurrent(), query.getEvery(), true), queryWrapper);
        IPage h5InfoVOIPage = H5InfoWrapper.build().pageVO(page);
        if (h5InfoVOIPage.getRecords() != null && !h5InfoVOIPage.getRecords().isEmpty()) {
            List<AuditVO> Records = (List<AuditVO>) h5InfoVOIPage.getRecords().stream().map(h5 -> {
                H5InfoVO h5InfoVO = JSONObject.parseObject(JSON.toJSONString(h5), H5InfoVO.class);
                if (h5InfoVO.getActivityId() != null && !h5InfoVO.getActivityId().isEmpty()) {
                    List<String> collect = Arrays.stream(h5InfoVO.getActivityId().split(",")).map(s -> {
                        ActivityInfo activityInfo = activityInfoMapper.selectOne(new QueryWrapper<ActivityInfo>()
                                .lambda()
                                .eq(true, ActivityInfo::getActivityId, s));
                        return activityInfo.getActivityName();
                    }).collect(Collectors.toList());
                    h5InfoVO.setActivityNames(collect);
                }
                AuditVO auditVO = new AuditVO();
                BeanUtils.copyProperties(h5InfoVO, auditVO);
                String department = this.baseMapper.getDepartment(auditVO.getCreatorId());
                auditVO.setDepartment(department);
                return auditVO;
            }).collect(Collectors.toList());
            h5InfoVOIPage.setRecords(Records);
        }
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        resultObject.setData(h5InfoVOIPage);
        return resultObject;
    }

    @Override
    public ResultObject saveH5Info(H5Info h5Info) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        // 当前时间
        String now = simpleDateFormat.format(date);
        ResultObject<Object> objectResultObject = new ResultObject<>();
        SysUser user = ReqestParamUtil.userInfo(request);
        h5Info.setCreatorId(user.getLoginName());
        h5Info.setCreatorName(user.getUserName());
        h5Info.setCreateTime(now);
        this.save(h5Info);
        objectResultObject.setCode("0000");
        objectResultObject.setMsg("添加成功");
        return objectResultObject;
    }

    @Override
    public ResultObject update(H5Info h5Info) {
        ResultObject objectResultObject = new ResultObject<>();
        this.update(new UpdateWrapper<H5Info>()
                .lambda()
                .eq(true, H5Info::getId, h5Info.getId())
                .setEntity(h5Info));
        objectResultObject.setCode("0000");
        objectResultObject.setMsg("成功");
        return objectResultObject;
    }

    @Override
    public ResultObject remove(String ids) {
        ResultObject objectResultObject = new ResultObject<>();
        List<String> strings = Arrays.stream(ids.split(",")).collect(Collectors.toList());
        this.removeByIds(strings);
        objectResultObject.setCode("0000");
        objectResultObject.setMsg("成功");
        return objectResultObject;
    }

    @Override
    public ResultObject audit(H5InfoDTO h5InfoDTO) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        // 当前时间
        String now = simpleDateFormat.format(date);
        SysUser user = ReqestParamUtil.userInfo(request);
        ResultObject objectResultObject = new ResultObject<>();
        this.update(new UpdateWrapper<H5Info>()
                .lambda()
                .eq(true, H5Info::getId, h5InfoDTO.getId())
                .set(true, H5Info::getStatus, h5InfoDTO.getStatus()));
        ApprovedMemo approvedMemo = new ApprovedMemo();
        approvedMemo.setH5Id(h5InfoDTO.getId());
        approvedMemo.setStatus(h5InfoDTO.getStatus());
        approvedMemo.setReason(h5InfoDTO.getReason());
        approvedMemo.setApproveId(user.getLoginName());
        approvedMemo.setApproveName(user.getUserName());
        approvedMemo.setCreateTime(now);
        iApprovedMemoService.save(approvedMemo);
        objectResultObject.setCode("0000");
        objectResultObject.setMsg("成功");
        return objectResultObject;
    }

    @Override
    public void untie(String id) {
        UpdateWrapper<H5Info> queryWrapper = new UpdateWrapper<>();
        queryWrapper
                .lambda()
                .setSql("activity_id=replace(`activity_id`,'" + id + "','')")
                .apply("find_in_set('" + id + "',activity_id)");
        this.update(queryWrapper);
    }


}
